package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserSearch extends Composite {

	public UserSearch() {

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel vpForm = new VerticalPanel();
		verticalPanel.add(vpForm);
		vpForm.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel hpName = new HorizontalPanel();
		vpForm.add(hpName);
		hpName.setSpacing(5);

		final Label lblName = new Label("Nome:");
		hpName.add(lblName);
		lblName.setStyleName("label");

		final TextBox txtName = new TextBox();
		hpName.add(txtName);
		txtName.setWidth("200");
		txtName.setStyleName("textbox");

		final HorizontalPanel hpUserName = new HorizontalPanel();
		vpForm.add(hpUserName);
		hpUserName.setSpacing(5);

		final Label lblUserName = new Label("Nome de Utilizador:");
		hpUserName.add(lblUserName);
		lblUserName.setStyleName("label");

		final TextBox txtUserName = new TextBox();
		hpUserName.add(txtUserName);
		txtUserName.setWidth("200");
		txtUserName.setStyleName("textbox");

		final HorizontalPanel hpEmail = new HorizontalPanel();
		vpForm.add(hpEmail);
		hpEmail.setSpacing(5);

		final Label lblEmail = new Label("Email:");
		hpEmail.add(lblEmail);
		lblEmail.setStyleName("label");

		final TextBox txtEmail = new TextBox();
		hpEmail.add(txtEmail);
		txtEmail.setWidth("200");
		txtEmail.setStyleName("textbox");

		final HorizontalPanel hpUserType = new HorizontalPanel();
		vpForm.add(hpUserType);
		hpUserType.setSpacing(5);

		final Label lblType = new Label("Tipo de Utilizador:");
		hpUserType.add(lblType);
		lblType.setStyleName("label");

		final ListBox lbType = new ListBox();
		hpUserType.add(lbType);
		
		lbType.addItem("Todos");
		lbType.addItem("Utilizador");
		lbType.addItem("Assistente");
		lbType.addItem("Administrador");

		final VerticalPanel vpList = new VerticalPanel();
		verticalPanel.add(vpList);
		vpList.setSpacing(5);
		vpList.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Label lblErro = new Label("");
		verticalPanel.add(lblErro);
		lblErro.setStyleName("erro");

		final HorizontalPanel hpButtons = new HorizontalPanel();
		verticalPanel.add(hpButtons);
		hpButtons.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Button btnSearch = new Button();
		hpButtons.add(btnSearch);
		btnSearch.setStyleName("button");
		btnSearch.setText("Procurar");
		btnSearch.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				vpList.clear() ;
				lblErro.setText("") ;
				ServiceAdministration.Util.getInstance().searchUser(txtName.getText(), txtUserName.getText(), txtEmail.getText(), lbType.getItemText(lbType.getSelectedIndex()), new UserSearchAsyncCallback(vpList,lblErro));
			}
		});
	}
	
	static class UserSearchAsyncCallback implements AsyncCallback
	{
		VerticalPanel vp ;
		Label lblErro ;
		UserSearchAsyncCallback(VerticalPanel vp, Label lblErro)
		{
			this.vp = vp ;
			this.lblErro = lblErro ;
		}
		
		public void onFailure(Throwable caught) {
			System.out.println("UserSearch :: FALHOU");
		}

		public void onSuccess(Object result) {
			ArrayList res = (ArrayList) result ;
			ArrayList idUser = (ArrayList) res.get(0);
			ArrayList unameUser = (ArrayList) res.get(1);
			ArrayList typeUser = (ArrayList) res.get(2);
			ArrayList nameUser = (ArrayList) res.get(3);
			ArrayList mailUser = (ArrayList) res.get(4);
			
			if (idUser.size() == 0 )
			{
				lblErro.setText("A pesquisa não devolveu resultados");
				return ;
			}
			
			Grid grid = new Grid(); 
			  
			System.out.println("Criou a grid");
			grid.resize(idUser.size()+1, 6);
			grid.setStyleName("grid");
			grid.setBorderWidth(1);
			grid.setCellSpacing(4);
			  			  
			Label lbl1 = new Label();
			lbl1.setText("#");
			lbl1.setStyleName("section_title");
			grid.setWidget(0, 0, lbl1);
			  
			Label lbl2 = new Label();
			lbl2.setText("UserName");
			lbl2.setStyleName("section_title");
			grid.setWidget(0, 1, lbl2);
			
			Label lbl3 = new Label();
			lbl3.setText("Nome");
			lbl3.setStyleName("section_title");
			grid.setWidget(0, 2, lbl3);
			 		  
			Label lbl4 = new Label();
			lbl4.setText("Email");
			lbl4.setStyleName("section_title");
			grid.setWidget(0, 3, lbl4);
			
			Label lbl5 = new Label();
			lbl5.setText("Tipo");
			lbl5.setStyleName("section_title");
			grid.setWidget(0, 4, lbl5);
			  
			Label lbl6 = new Label();
			lbl6.setText("");
			lbl6.setStyleName("section_title");
			grid.setWidget(0, 5, lbl6);
			  
			for (int i = 0 ; i < idUser.size(); i++)
			{
				Label lblId = new Label();
				lblId.setText(idUser.get(i).toString());
				lblId.setStyleName("label");
				grid.setWidget(i+1, 0, lblId);
				
				Label lblUname = new Label();
				lblUname.setText(unameUser.get(i).toString());
				lblUname.setStyleName("label");
				grid.setWidget(i+1, 1, lblUname);
				
				Label lblName = new Label();
				lblName.setText(nameUser.get(i).toString());
				lblName.setStyleName("label");
				grid.setWidget(i+1, 2, lblName);
				
				Label lblMail = new Label();
				lblMail.setText(mailUser.get(i).toString());
				lblMail.setStyleName("label");
				grid.setWidget(i+1, 3, lblMail);
				
				Label lblType = new Label();
				lblType.setText(typeUser.get(i).toString());
				lblType.setStyleName("label");
				grid.setWidget(i+1, 4, lblType);
				  
				Button btnRemove = new Button("Remover");
				btnRemove.setStyleName("button");
				btnRemove.addClickListener(new UserRemoveOnClick((Integer)idUser.get(i),unameUser.get(i).toString(),typeUser.get(i).toString(),lblErro));
				grid.setWidget(i+1, 5, btnRemove);
			}
			  
			vp.add(grid);
		}
	}
}
